<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h3>数组实现队列</h3><br/>
    <h3>击鼓传花</h3>
    <script>
        class Queue {

            constructor() {
                    this.item = []; //数组的初始化
                    // this.item ={};

                }
                //添加
            enqueue(element) {
                    this.item.push(element);
                }
                //删除
            dequeue() {
                return this.item.shift();

            }
            peek() {
                if (this.isEmpty()) {
                    return undefined;
                } else {
                    return this.item[0];
                }

            }
            isEmpty() {
                return this.item.length === 0;
            }
            size() {
                return this.item.length;
            }

        }

        function playgame(playerList, num) {
            const queue = new Queue();
            for (let i = 0; i < playerList.length; i++) {
                queue.enqueue(playerList[i]);
            }
            while (queue.size() > 1) {
                for (let i = 0; i < num - 1; i++) {
                    queue.enqueue(queue.dequeue());
                }
                let out = queue.dequeue();
                console.log("淘汰：" + out);

            }
            console.log("victor " + queue.item);


        }

        const player = ["a", "b", "c", "d", "e", "f"];
        const num = 3;
        playgame(player, num);
        //队列
        // united kingdom
        /*    const queue = new Queue();
           queue.enqueue("cn"); //china
           queue.enqueue("us");
           queue.enqueue("uk");
           queue.enqueue("jp");

           console.log(queue.item);
           console.log(queue.dequeue());
           console.log(queue.isEmpty());
           console.log(queue.peek());
           console.log(queue.size()); */
    </script>

</body>

</html>